Certified Java Programmer Mock Exam


Question 1


class Basics {
  static int x=1;
  void m(int i) {x++;i++;}
  public static void main (String[] args) {
    int y=3;
    m(y);
    System.out.println(x + "," + y);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,3
d. Prints: 2,4
e. Run time Exception
f. Compiler Error
g. None of the Above

Question 2


class Basics {
  private static int x=1;
  static void m(int i) {x++;i++;}
  public static void main (String[] args) {
    int y=3;
    m(y);
    System.out.println(x + "," + y);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,3
d. Prints: 2,4
e. Run time Exception
f. Compiler Error
g. None of the Above

Question 3


interface A {
  final void m6();        // 1
  synchronized void m7(); // 2
  strictfp void m8();     // 3
  native void m9();       // 4
}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. None of the above.

Question 4



Which of the following are modifiers that can be applied to a method declaration within an interface?

a. abstract
b. final
c. private
d. protected
e. public
f. None of the above.

Question 5


interface A {
  int a = 1;                        // 1
  public int b = 2;                 // 2
  public static int c = 3;          // 3
  public static final int d = 4;    // 4
}

Which of the field declarations result in a compile-time error?

a. 1
b. 2
c. 3
d. 4
e. None of the above.

Question 6


class L {
  static String m(float i) {return "float";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    int a = 1;
    long b = 2;
    System.out.print(m(a)+","+ m(b));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 7


 1. interface I1 {}
 2. interface I2 {}
 3. class Base implements I1 {}
 4. class Sub extends Base implements I2 {}
 5.
 6. class Orange {
 7.   public static void main(String args[]) {
 8.     Base base = new Base();
 9.     I1 i1 = base;
10.     Sub sub = (Sub)base;
11.   }
12. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 9.
b. Runtime error at line 9.
c. Compiler error at line 10.
d. Runtime error at line 10.
e. Compiles and runs without error.

Question 8


class A {
  void m1(A a) {System.out.print("A");}
}
class B extends A {
  void m1(B b) {System.out.print("B");}
}
class C extends B {
  void m1(C c) {System.out.print("C");}
}
class D {
  public static void main(String[] args) {
    A c1 = new C();
    A c2 = new C();
    A c3 = new C();
    C c4 = new C();
    c1.m1(c4);
    c2.m1(c4);
    c3.m1(c4);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 9


1.  interface I1 {}
2.  interface I2 {}
3.  class Base implements I1 {}
4.  class Sub extends Base implements I2 {}
5.  class Silver {
6.    public static void main(String []args) {
7.      Base[] base = {new Base()};
8.      Sub sub[] = new Sub[1];
9.      Object obj = base;
10.     sub = (Sub[])obj;
11.     I1 []i1 = (I1[])obj;
12.   }
13. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 8.
b. Runtime error at line 8.
c. Compiler error at line 9.
d. Runtime error at line 9.
e. Compiler error at line 10.
f. Runtime error at line 10.
g. Compiler error at line 11.
h. Runtime error at line 11.
i. Compiles and runs without error.

Question 10


class P {
  static void printS1(){System.out.print("P.printS1 ");}
  void printS2() {System.out.print("P.printS2 ");}
  void printS1S2(){printS1();printS2();}
}

class Q extends P {
  static void printS1(){System.out.print("Q.printS1 ");}
  void printS2(){System.out.print("Q.printS2 ");}
  public static void main(String[] args) {
    new Q().printS1S2();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: P.printS1 P.printS2
b. Prints: P.printS1 Q.printS2
c. Prints: Q.printS1 P.printS2
d. Prints: Q.printS1 Q.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 11


class A {}
class B extends A {}
class C extends B {}
class D {
  void m1(A a) {System.out.print("A");}
  void m1(B b) {System.out.print("B");}
  void m1(C c) {System.out.print("C");}
  public static void main(String[] args) {
    A c1 = new C();
    B c2 = new C();
    C c3 = new C();
    D d = new D();
    d.m1(c1);
    d.m1(c2);
    d.m1(c3);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 12


class A {}
class B extends A {}
class C extends B {
  static void m(A x, A y) {System.out.print("AA");}
  static void m(A x, B y) {System.out.print("AB");}
  static void m(B x, A y) {System.out.print("BA");}
  static void m(B x, B y) {System.out.print("BB");}
  public static void main(String[] args) {
    A a;
    B b;
    m(null,null);
    m(a=null,b=null);
    m(b, a);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: BBABAB
b. Prints: BBABBA
c. Prints: BBBBAB
d. Prints: BBBBBA
e. Prints: BBBBBB
f. Compiler error.
g. Runtime error.
h. None of the Above

Question 13


 1. interface I1 {}
 2. interface I2 {}
 3. class Base implements I1 {}
 4. class Sub extends Base implements I2 {}
 5.
 6. class Red {
 7.   public static void main(String args[]) {
 8.     Sub s1 = new Sub();
 9.     I2 i2 = s1;
10.     I1 i1 = s1;
11.     Base base = s1;
12.     Sub s2 = (Sub)base;
13.   }
14. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 9.
b. Compiler error at line 10.
c. Compiler error at line 11.
d. Compiler error at line 12.
e. Runtime error.
f. Compiles and runs without error.

Question 14


1.  interface I10 {
2.    String name = "I10";
3.    String s10 = "I10.s10";
4.  }
5.  interface I20 {
6.    String name = "I20";
7.    String s20 = "I20.s20";
8.  }
9.  class C20 implements I10, I20 {
10.   public static void main(String[] args) {
11.     System.out.print(I10.s10+",");
12.     System.out.print(I20.s20+",");
13.     System.out.print(I20.name);
14.   }
15. }

What is the result of attempting to compile and run the above program?

a. Prints: I10.s10,I20.s20,I10
b. Prints: I10.s10,I20.s20,I20
c. Prints: I10.s10,I20.s20,
d. Prints: I10.s10,I20.s20,null
e. Compiler error at line 9.
f. Compiler error at line 11.
g. Compiler error at line 12.
h. Compiler error at line 13.
i. Runtime Error
j. None of the Above

Question 15


class L {
  static String m(int i) {return "int";}
  static String m(float i) {return "float";}
  public static void main (String[] args) {
    long a = 1;
    double b = 2;
    System.out.print(m(a)+","+ m(b));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compile-time error.
f. Run-time error.
g. None of the Above

Question 16


class T {
  private int i1, i2;
  void printI1I2() {System.out.print("T: i1=" + i1 + ", i2=" + i2);}
  T(int i1, int i2) {
    this.i1=i1;this.i2=i2;
  }
}

class U extends T {
  private int i1, i2;
  void printI1I2() {System.out.print("U: i1=" + i1 + ", i2=" + i2);}
  U(int i1, int i2) {
    this.i1=i1;this.i2=i2;
  }
  public static void main(String[] args) {
    T t = new U(1,2);
    t.printI1I2();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: U: i1=1, i2=2
b. Prints: T: i1=1, i2=2
c. Prints: U: i1=null, i2=null
d. Prints: T: i1=null, i2=null
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 17


class A {
  void m1(A a) {System.out.print("A");}
}
class B extends A {
  void m1(B b) {System.out.print("B");}
}
class C extends B {
  void m1(C c) {System.out.print("C");}
}
class D {
  public static void main(String[] args) {
    A a1 = new A();
    A b1 = new B();
    A c1 = new C();
    C c4 = new C();
    a1.m1(c4);
    b1.m1(c4);
    c1.m1(c4);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 18


class A {public void m1() {System.out.print("A1");}}
class B extends A {
  public void m1() {System.out.print("B1");}
  public void m2() {System.out.print("B2");}
}
class C {
  public static void main(String[] args) {
    A a1 = new B();
    a1.m1();
    a1.m2();
    ((B)a1).m1();
    ((B)a1).m2();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: A1B2B1B2
b. Prints: B1B2B1B2
c. Compiler Error.
d. Runtime Error.
e. None of the Above.


Copyright © 2002, Dan Chisholm
All rights reserved.
1